﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel;
using System.ServiceModel.Description;
using System.ServiceModel.Security;
using System.Text;
using System.Threading.Tasks;
using Header;
using System.Diagnostics;
using System.IdentityModel.Policy;
using Header.Security;
using Server.Data;


namespace Server
{
	class Program
	{
		static void Main(string[] args)
		{
			Console.WriteLine("Connecting to database...");

			//NOTE: This is just for demo purpose, in deployment phase, those hard string should be stored in a config file
			var connector = new MySqlConnector(username: "root", password: "root", server: "localhost", database: "chauluc");
			connector.Connect();

			Console.WriteLine("Starting up...");

			var address = new Uri("http://localhost:8733/Design_Time_Addresses/LibMgt");
			var service = new ChauLuc(connector.Connection);
			var host = new ServiceHost(service, address);

			var http = new BasicHttpBinding();
			//http.Security.Mode = BasicHttpSecurityMode.TransportCredentialOnly;
			//http.Security.Transport.ClientCredentialType = HttpClientCredentialType.Basic;
			//host.Credentials.UserNameAuthentication.UserNamePasswordValidationMode = UserNamePasswordValidationMode.Custom;
			//host.Credentials.UserNameAuthentication.CustomUserNamePasswordValidator = new MyCustomValidator(connector.Connection);

			host.Authorization.ServiceAuthorizationManager = new ServiceAuthorizationManager();
			var policies = new List<IAuthorizationPolicy>();
			policies.Add(new ChauLucAuthorizationPolicyV1());
			host.Authorization.ExternalAuthorizationPolicies = policies.AsReadOnly();
			host.AddServiceEndpoint(typeof(ILogin), http, address);

			//DO NOT EARSE
			//Set custom Authorization policy
			//var policies = new List<IAuthorizationPolicy>();
			//policies.Add( new MyAuthorizationPolicy() );
			//host.Authorization.ExternalAuthorizationPolicies = policies.AsReadOnly();


			//enable fault report
			ServiceDebugBehavior debug = host.Description.Behaviors.Find<ServiceDebugBehavior>();

			// if not found - add behavior with setting turned on 
			if (debug == null)
			{
				host.Description.Behaviors.Add(new ServiceDebugBehavior() { IncludeExceptionDetailInFaults = true });
			}
			else
			{
				// make sure setting is turned ON
				if (!debug.IncludeExceptionDetailInFaults)
				{
					debug.IncludeExceptionDetailInFaults = true;
				}
			}


			// checking and publishing meta data
			var smb = host.Description.Behaviors.Find<ServiceMetadataBehavior>();
			if (smb == null)
			{
				smb = new ServiceMetadataBehavior();
				smb.HttpGetEnabled = true;
				host.Description.Behaviors.Add(smb);
			}


			try
			{

				Console.WriteLine("Opening service...");
				host.Open();

				Console.WriteLine("Service is listening on {0}...", address);
				Console.WriteLine("To terminate press ENTER");
				Console.ReadLine();
			}
			catch (Exception ex)
			{
				Console.WriteLine("Service terminated with exception {0}", ex.ToString());
				Console.ReadKey();
			}
			finally
			{
				host.Close();
			}


		}
	}
}
